﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodWorx.CMS.TemplateParser
{
    [Serializable]
    public class TemplateDocument : TemplateNode
    {
        internal IEnumerable<TemplateElement> FindElements(string name, bool ignoreCase)
        {
            foreach(var element in FlattenNodes<TemplateElement>())
            {
                //
                //  ok, this is the name
                if ((ignoreCase && element.Name.ToLower() == name.ToLower()) ||
                    !ignoreCase && element.Name == name)
                {
                    //  they match
                    yield return element;
                }

            }
        }

        public string OuterXml
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (var element in this.Children)
                {
                    if (element is TemplateElement)
                    {
                        sb.Append(((TemplateElement)element).OuterXml);
                    }
                    else if (element is TemplateText)
                    {
                        sb.Append(((TemplateText)element).Text);
                    }
                }
                return sb.ToString();
            }
        }

     
    }
}
